package leetcode;

public class MaxConsecutiveOnes {

	public static void main(String[] args) {
		MaxConsecutiveOnes object = new MaxConsecutiveOnes();
		int[] nums = {1,1,0,1,1,1, 0};
		object.findMaxConsecutiveOnes(nums);
	}
	
	//我自己想的
	public int findMaxConsecutiveOnes(int[] nums) {
        int length = 0;
        if(nums == null || nums.length <= 0){
            return 0;
        }
        Integer start = null;
        Integer end = null;
        for(int i = 0; i < nums.length; i++){
            if(start == null && end == null){
            	if(nums[i] == 1){
            		start = i;
            		end = i;
            	}
            }else if(nums[i] == 1){
                end = i;
            }else if(nums[i] == 0){
                length = Math.max(length, end - start + 1);
                start = null;
                end = null;
            }
        }
        if(!(start == null && end == null)){
        	length = Math.max(length, end - start + 1);
        }
        return length;
    }
	
	//别人的方法
	public int findMaxConsecutiveOnes2(int[] nums) {
        if(nums == null || nums.length <= 0){
            return 0;
        }
        int length = 0;
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
			if(nums[i] == 0){
				max = 0;
			}else{
				max++;
			}
			length = Math.max(length, max);
		}
        return length;
	}
}
